<template>
  <el-button size="small" :disabled="disabledState" @click="downexcel">导出表格</el-button>
</template>

<script>
import downloadJson from "./downloadJson";

export default {
  name: "excelDowmLoadBtn",
  props: ['disabledState', 'downloadOption'],
  methods: {

    // 导出excel文件
    downexcel() {
      let request = `${this.$BASE_URL}/admin/${this.downloadOption.url}?${this.querystringFn()}`
      let excelName = this.fileExcelName()
      this.$http({
        method: 'get',
        url: request,
        responseType: 'arraybuffer'
      }).then(function (response) {
        // let filename = this.editForm.phone + '物料（明细）.xlsx'
        // 导出的名称
        this.fileDownload(response.data, excelName)
      }.bind(this))
          .catch(
              function (error) {
                alert('网络请求出错')
              }
          )
    },
    fileDownload(data, fileName) {
      let blob = new Blob([data], {
        type: 'application/octet-stream'
      })
      let filename = fileName + '.xlsx' || 'filename.xlsx'
      if (typeof window.navigator.msSaveBlob !== 'undefined') {
        window.navigator.msSaveBlob(blob, filename)
      } else {
        let blobURL = window.URL.createObjectURL(blob)
        let tempLink = document.createElement('a')
        tempLink.style.display = 'none'
        tempLink.href = blobURL
        tempLink.setAttribute('download', filename)
        if (typeof tempLink.download === 'undefined') {
          tempLink.setAttribute('target', '_blank')
        }
        document.body.appendChild(tempLink)
        tempLink.click()
        document.body.removeChild(tempLink)
        window.URL.revokeObjectURL(blobURL)
      }
    },
    querystringFn() {
      let query_n = JSON.parse(JSON.stringify(this.downloadOption.query))
      let query_ = []
      if (query_n['regionTime']) {
        delete query_.start_time
        delete query_.end_time
      }
      for (let index in query_n) {
        if (index === 'regionTime') {
          query_.push(`start_time=${this.downloadOption.query[index][0]}`)
          query_.push(`end_time=${this.downloadOption.query[index][1]}`)
        }
        if (index !== 'regionTime') {
          query_.push(`${index}=${this.downloadOption.query[index]}`)
        }
      }
      return query_.join('&')
    },
    fileExcelName() {
      let fileName = []
      for (let index of this.downloadOption.fileName) {
        if (index === 'regionTime') {
          fileName.push(`${this.downloadOption.query[index][0]}至${this.downloadOption.query[index][1]}`)
        }else{
          fileName.push(`${this.downloadOption.query[index]}`)
        }
      }
      let name = fileName.join('') + this.downloadOption.name
      if (this.downloadOption.className) {
        name = fileName.join('') + this.downloadOption.className + this.downloadOption.name
      }
      if (this.downloadOption.semesterName) {
        name = fileName.join('') + this.downloadOption.semesterName + this.downloadOption.name
      }
      return name
    }
  }
}
</script>

<style scoped>

</style>
